from PyQt5 import QtCore
from PyQt5.QtCore import QTimer, pyqtSignal
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QApplication, QMainWindow, QStackedWidget, QWidget, QVBoxLayout, QPushButton, QLabel, \
    QLineEdit, QTextEdit
from utils.httpUtil import RequestUtil

class PageOne(QWidget):
    page_size = QtCore.pyqtSignal(int)

    def __init__(self):
        super().__init__()

        self.label = QLabel(self.get_soil())
        self.label.setWordWrap(True)
        font = QFont()
        font.setPointSize(20)
        self.label.setFont(font)
        self.init_ui()

    def init_ui(self):
        layout = QVBoxLayout()
        btn = QPushButton('重新获取心灵鸡汤')
        btn.clicked.connect(self.reset_text)
        layout.addWidget(self.label)
        layout.addWidget(btn)
        self.setLayout(layout)
        self.setGeometry(100, 100, 1280, 960)

    def reset_text(self):
        # 这里假定有一个方法可以访问到 QStackedWidget 并切换页面
        # 例如，通过信号或回调函数
        self.label.setText(self.get_soil())

    @staticmethod
    def get_soil():
        """
        再次获取鸡汤
        :return: 心灵鸡汤文本
        """
        url = 'https://apis.juhe.cn/fapig/soup/query?key=fddb96963ca4759e3db4cb8b56cdadf4'
        http = RequestUtil()
        try:
            soil = http.get(url)['result']['text']
            if len(soil) > 0:
                return soil
            else:
                return '错误：请求获取聚合[每日心灵鸡汤语录异常]'
        except Exception as e:
            return '错误：请求获取聚合[每日心灵鸡汤语录异常]'
